#include "enter_leave_widget.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QEvent>

#include "Event/labelx.h"

EnterLeaveWidget::EnterLeaveWidget(QWidget *parent)
	: QWidget{parent}
{
	QVBoxLayout * mainLayout=new QVBoxLayout();
	LabelX * label=new LabelX("鼠标进入/离开");
	label->setStyleSheet(R"(background-color: black;color white;)");
	label->setFrameShape(QFrame::Box);
	label->setFixedHeight(50);
	label->setAlignment(Qt::AlignCenter);

	labelx=new QLabel();
	labelx->setStyleSheet(R"(background-color: green;color white;)");
	labelx->setFrameShape(QFrame::Box);
	labelx->setFixedHeight(50);
	labelx->setAlignment(Qt::AlignCenter);

	// labelx的事件过滤器
	labelx->installEventFilter(this);

	mainLayout->addWidget(label);
	mainLayout->addWidget(labelx);
	this->setLayout(mainLayout);
}

bool EnterLeaveWidget::eventFilter(QObject *watched, QEvent *event)
{
	if(watched == labelx){
		if(event->type() == QEvent::Enter){
			labelx->setText(QString("Enter Event:" + QString::number(++enternum)));
		}else if(event->type() == QEvent::Leave){
			labelx->setText(QString("Leave Event:" + QString::number(++leavenum)));
		}
	}
	return QWidget::eventFilter(watched,event);
}
